//
//  EditComponentFactory.m
//  AFNetworking
//
//  Created by abc on 2018/4/10.
//

#import "EditComponentFactory.h"

#import "CoreDatePickerView.h"

#import <base/base.h>

#import <QMUIKit/QMUIKit.h>

@interface EditComponentFactory ()

@end

@implementation EditComponentFactory

+ (CoreEditComponent *)emailEditComponent {
    CoreEditComponent *editComponent = [CoreEditComponent defaultEditComponent];
    editComponent.textCleaarAble(YES).placeHolderColor([UIColor tx_colorWithHex:0xA7A7A7]).padding_right(PADDING_FOR_CLEAR);
    editComponent.keyboardType(UIKeyboardTypeEmailAddress).autoCapitalization(NO);
    editComponent.placeHolderText(@"Email");
    @weakify(editComponent);
    editComponent.vertifyBlock = ^NSString *() {
        @strongify(editComponent);
        NSString *resultString = nil;
        NSString *input = [editComponent.currentText stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
        if (input.length<=0)
        {
            resultString = @"Please enter an email address.";
        }
        else if (![input isValidEmail] || [input isContainSpecialCharacters])
        {
            resultString = @"Please check your email format.";
        }
        editComponent.invalid = resultString ? YES : NO;
        return resultString;
    };
    return editComponent;
}

+ (CoreEditComponent *)passwordEditComponent {
    CoreEditComponent *editComponent = [CoreEditComponent defaultEditComponent];
    editComponent.maxInputLenght(40).textCleaarAble(YES).passwordInput(YES).placeHolderColor([UIColor tx_colorWithHex:0xA7A7A7]).padding_right(10);
    editComponent.placeHolderText(@"Password");
    
    UILabel *hideRoShow = [[UILabel alloc] qmui_initWithFont:UIFontMake(12.f) textColor:[UIColor tx_colorWithHex:0x333333]];
    hideRoShow.text = @"SHOW";
    hideRoShow.textAlignment = NSTextAlignmentCenter;
    editComponent.volatileView(hideRoShow).volatileSize(CGSizeMake(38.f, 30.f));
    @weakify(editComponent);
    editComponent.volatileAction(^(){
        @strongify(editComponent);
        BOOL isHide = [hideRoShow.text isEqualToString:@"SHOW"];
        editComponent.passwordInput(!isHide);
        hideRoShow.text = isHide ? @"HIDE" : @"SHOW";
    });
    
    editComponent.vertifyBlock = ^NSString *() {
        @strongify(editComponent);
        NSString *resultString = nil;
        NSString *input = editComponent.currentText;
        if (input.length<=0)
        {
            resultString = @"Please enter your password.";
        }
        else if (![input isValidPassword])
        {
            resultString = @"Your password should be at least 5 characters.";
        }
        editComponent.invalid = resultString ? YES : NO;
        return resultString;
    };
    return editComponent;
}

+ (CoreEditComponent *)datePickerEditComponentForStyle:(NSDateFormatterStyle)style {
    return [self datePickerEditComponentWithRenderBlock:^CoreDatePickerView *(CoreDatePickerView *picker) {
        return picker.displatStyle(style);
    }];
}

+ (CoreEditComponent *)datePickerEditComponentFormat:(NSString *)format {
    return [self datePickerEditComponentWithRenderBlock:^CoreDatePickerView *(CoreDatePickerView *picker) {
        return picker.displatFormat(format);
    }];
}

+ (CoreEditComponent *)datePickerEditComponentForMode:(UIDatePickerMode)mode {
    return [self datePickerEditComponentWithRenderBlock:^CoreDatePickerView *(CoreDatePickerView *picker) {
        return picker.displayMode(mode);
    }];
}

#pragma mark - date picker view
+ (CoreEditComponent *)datePickerEditComponentWithRenderBlock:(CoreDatePickerView *(^)(CoreDatePickerView *))renderBlock {
    CoreEditComponent *editComponent = [CoreEditComponent defaultEditComponent];
    editComponent.textTintColor(UIColorClear).placeHolderColor([UIColor tx_colorWithHex:0xA7A7A7]).placeHolderRiseable(NO).placeHolderText(@"Wedding Date");
    @weakify(editComponent);
    editComponent.tapResponseBlock = ^{
        @strongify(editComponent);
        renderBlock([CoreDatePickerView datePickerViewWithTitle:@"Date of Event"
                                                       dateType:UIDatePickerModeDate
                                                     clearBlock:^() {
                                                         editComponent.text(@"");
                                                     }
                                                    resultBlock:^(NSString *selectValue) {
                                                        editComponent.text(selectValue);
                                                    }]).defaultSelValue(editComponent.currentText).show();
    };
    editComponent.vertifyBlock = ^NSString *(){
        @strongify(editComponent);
        NSString *resultString = nil;
        if (editComponent.currentText.length <=0 ) {
            resultString = @"Please enter your wedding date.";
        }
        editComponent.invalid = resultString ? YES : NO;
        return resultString;
    };
    return editComponent;
}

@end
